//
//  HHMeViewController.m
//  HHBroker
//
//  Created by He Wei on 8/26/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHMeViewController.h"
#import "HHMEInfoModel.h"
#import "HHWebViewController.h"
@interface HHMeViewController ()

@end

@implementation HHMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupData];
    // Do any additional setup after loading the view.
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    self.hidesBottomBarWhenPushed = NO;
//
//}

//-(void)viewWillDisappear:(BOOL)animated
//{
//    self.hidesBottomBarWhenPushed = YES;
//}



#pragma mark - Delegate
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseCell = @"reuseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCell];
    }
    
    HHMEInfoModel *model = self.dataArray[indexPath.section][indexPath.row];
    cell.textLabel.text = model.title;
    cell.imageView.image = [UIImage imageNamed:model.iconName];
    return cell;
}



#pragma mark - UITableViewDelegate
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    self.index = indexPath;
    HHMEInfoModel *model = self.dataArray[indexPath.section][indexPath.row];
    Class class = NSClassFromString(model.action);
    if ([class isSubclassOfClass:[UIViewController class]]) {
        HHMeViewController *viewController = [[class alloc] init];
        viewController.title = model.title;
        viewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:viewController animated:YES];
        return;
    }
    SEL action = NSSelectorFromString(model.action);
    if ([self respondsToSelector:action]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "- Warc - performSelector - leaks"
        [self performSelector:action];
#pragma clang diagnostic pop
        return;
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    

    
}

#pragma mark  - Fun
- (void)toggleToWebView
{
    HHWebViewController *web = [[HHWebViewController alloc] init];
    web.urlString = @"http://v.jiayuan.com/broker/kpi/";
    web.naviTitle = @"绩效管理";
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];

}






#pragma mark - Setter && Getter
- (void)setupData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"tab_me_propertyList" ofType:@"plist"];
    NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
    for (NSArray *array in dataArray) {
        NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:3];
        for (NSDictionary *dic in array) {
            HHMEInfoModel *model = [[HHMEInfoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [sectionArray addObject:model];
        }
        [self.dataArray addObject:sectionArray];
    }
    
    [self.tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
